<?php

namespace App\Http\Resources\SystemNotify;

use App\Http\Resources\BaseResource;
use App\Models\System\SystemNotifyCenterModel;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserNotifyListResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            "id"                   => $this->id,
            "notifyCenterId"       => $this->notifyCenterId,
            "notifyCenterType"     => $this->notifyCenterType,
            "notifyCenterTypeText" => SystemNotifyCenterModel::NOTIFY_TYPE_MAPPING[$this->notifyCenterType] ?? "",
            'title'                => $this->notifyCenterRel->title,
            "userId"               => $this->userId,
            "createdAt"            => $this->createdAt->timestamp,
            "createdAtText"        => $this->createdAt->format("m-d"),
            'url'                  => $this->notifyCenterRel->url,
            'summary'              => $this->notifyCenterRel->summary,
            'pushType'             => $this->notifyCenterRel->pushType,
            'pushTargetId'         => $this->notifyCenterRel->pushTargetId,
            'readAt'               => $this->readAt,
            'isContent'            => empty(self::clearHtmlLabel($this->notifyCenterRel->content)) ? 0 : 1,
        ];
    }

    public static function clearHtmlLabel($str)
    {
        $str = str_replace('<p>', '', $str);
        $str = str_replace('</p>', '', $str);
        $str = str_replace('<br>', '', $str);

        return $str;
    }
}
